package com.hejun.student.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hejun.student.entity.Student;
import com.hejun.student.service.StudentService;
import com.hejun.student.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    //分页带条件查询
    @GetMapping
    public ResultVo page(String studentName, @RequestParam(defaultValue = "1") Integer pageNum) {
        Page<Student> studentPage = studentService.getPage(studentName, pageNum);
        return ResultVo.success("", studentPage);
    }

    @PostMapping()
    public ResultVo saveOrUpdate(@RequestBody Student student) {
        boolean bl = studentService.saveOrUpdate(student);
        String str = "";
        //postman没有传id哪来的id？而且还是新增的正确id难道是mybatisPlus的？
        if (student.getId() != null) {
            str = "修改";
        } else {
            str = "成功";
        }
        if (bl) {
            return ResultVo.success(str + "成功", null);
        }

        return ResultVo.error(str + "失败");
    }

    @DeleteMapping("/{id}")
    public ResultVo delete(@PathVariable Integer id){
        if (studentService.removeById(id)) {
            return ResultVo.success("删除成功",null);
        }
        return ResultVo.error("删除失败");
    }
}
